package org.btik.light.mobile.ui.devs.mecanum.wheel.car;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.Nullable;
import org.btik.context.SimpleContext;
import org.btik.light.mobile.R;
import org.btik.light.mobile.common.handler.adaptor.SpeedHandler;
import org.btik.light.mobile.meta.VideoChannelType;
import org.btik.light.mobile.ui.devs.VideoCapability;
import org.btik.light.mobile.ui.factory.DevPanelActivity;
import org.btik.video.server.VideoServer;

import static org.btik.light.mobile.platform.conf.ContextGlobalNames.VIDEO_SERVER;
import static org.btik.light.mobile.ui.devs.mecanum.wheel.car.MecanumWheelCarHandlers.*;


/**
 * @author lustre
 * @since 2022/12/20 15:45
 */
public class MecanumWheelCarActivity extends DevPanelActivity implements VideoCapability {
    private TextView speedShow;
    private SeekBar seekBar;

    private Switch videoSwitch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.mecanum_wheel_car);
        bindAction();
        loadVideo();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void bindAction() {
        speedShow = findViewById(R.id.speed);
        seekBar = findViewById(R.id.speed_bar);
        setSpeedShow(SpeedHandler.MAX_SPEED, true);
        SPEED_HANDLER.setSpeedShow(this::setSpeedShow);
        seekBar.setOnSeekBarChangeListener(SPEED_HANDLER);
        bind(R.id.speed_up, SPEED_HANDLER::speedUp);
        bind(R.id.speed_down, SPEED_HANDLER::speedDown);
        bind(R.id.maxSpeed, SPEED_HANDLER::goPikachu);
        bind(R.id.up_, GO_FORWARD);
        bind(R.id.left, TO_GO_LEFT);
        bind(R.id.right, TO_GO_RIGHT);
        bind(R.id.down, GO_BACKWARD);
        bind(R.id.fl, TO_GO_FRONT_LEFT);
        bind(R.id.rl, TO_GO_REAR_LEFT);
        bind(R.id.rf, TO_GO_RIGHT_FRONT);
        bind(R.id.rr, TO_GO_RIGHT_REAR);
        bind(R.id.stop, TO_STOP);
        bind(R.id.tl, TO_TURN_LEFT);
        bind(R.id.tr, TO_TURN_RIGHT);
        bind(R.id.light_switch, LIGHT_SWITCH);

    }

    private void setSpeedShow(int progress, boolean setSeekBar) {
        speedShow.setText(String.valueOf(progress));
        if (setSeekBar) {
            seekBar.setProgress(progress);
        }
    }

    private void loadVideo() {
        videoSwitch = bind(R.id.video_switch, VIDEO_SWITCH);
        SurfaceView carVideo = findViewById(R.id.video);
        bindChannel(carVideo.getHolder(),defaultDevMo.getVideoChannelType());
        videoSwitch.setVisibility(View.VISIBLE);
        carVideo.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        VIDEO_0FF.accept(videoSwitch);
        super.onDestroy();
    }
}
